//
//  CustomAlertSheetView.m
//  tv_shop
//
//  Created by myTV on 16/7/25.
//  Copyright © 2016年 peraytech. All rights reserved.
//
#define CustomAlertItem_Height 46
#import "CustomAlertSheetView.h"

@interface CustomAlertSheetView()
@property (nonatomic,strong) NSArray<NSString *> *titles;
@property (nonatomic) CGFloat Viewheight;
@property (nonatomic) CGFloat ViewWidth;
@end
@implementation CustomAlertSheetView

- (instancetype)initWithCustomArr:(NSArray *)arrTitle andSubView:(UIView *)subView{
//    CGRect frame = [[UIScreen mainScreen] bounds];
    
    self = [super initWithFrame:subView.bounds];
    self.Viewheight = subView.bounds.size.height;
    self.ViewWidth = subView.bounds.size.width;
    if (self) {
        [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]];
        self.titles = arrTitle;
        [self setSubView:arrTitle];
    }
    return self;
}

- (void)setSubView:(NSArray *)arrTitle{
    [self addBottomCancel];
    [self addSubTitles:arrTitle];
    //轻拍消失
    UITapGestureRecognizer *recognizer =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disapperView)];
    [self addGestureRecognizer:recognizer];
}
- (void)addBottomCancel
{
    UIButton *cancleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.Viewheight - 60, self.ViewWidth, 60)];
    cancleButton.backgroundColor = [UIColor whiteColor];
    [cancleButton setTitleColor:[UIColor colorWithRed:255.0/255 green:54.0/255 blue:87.0/255 alpha:1.0] forState:UIControlStateNormal];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    
    [cancleButton addTarget:self action:@selector(disapperView) forControlEvents:UIControlEventTouchUpInside];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.ViewWidth, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"F1F1F1"];
    [cancleButton addSubview:line];
    [self addSubview:cancleButton];
}
- (void)addSubTitles:(NSArray <NSString *>*)arrTitle
{
    CGFloat origin_y = self.Viewheight - 60 - CustomAlertItem_Height*arrTitle.count;
    for (int i=0; i<arrTitle.count; i++) {
        //按钮
        UIButton *customeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *btnStr = [NSString stringWithFormat:@"%@",[arrTitle objectAtIndex:i]];
        [customeButton setTitle:btnStr forState:UIControlStateNormal];
        customeButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [customeButton setTitleColor:[UIColor colorWithRed:50.0/255 green:50.0/255 blue:50.0/255 alpha:1.0] forState:UIControlStateNormal];
//        postionOfY +
        
        customeButton.frame = CGRectMake(0, origin_y + CustomAlertItem_Height*i, self.ViewWidth, CustomAlertItem_Height);
        [customeButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        customeButton.tag = 100 + i;
        [customeButton setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:customeButton];
        
        //分割线
        if (i==(arrTitle.count-1)) {
//            DLog(@"相等，不添加分割线");
        }else{
            UIView *line    = [[UIView alloc] initWithFrame:CGRectMake(14, CustomAlertItem_Height - 0.5, self.Viewheight-28, 0.5)];
            line.backgroundColor = [UIColor colorWithHexString:@"F1F1F1"];
            [customeButton addSubview:line];
        }
    }
}

- (void)btnClick:(UIButton *)sender{
    NSInteger clickIndex = sender.tag -100;
    if ([self.delegate respondsToSelector:@selector(selectBtnIndex:)]) {
        [self.delegate selectBtnIndex:clickIndex];
        
    }
    NSString *title = @"";
    if (self.titles.count >= clickIndex) {
        title = self.titles[clickIndex];
    }
    if (self.selectIndex) {
        self.selectIndex(clickIndex, title);
    }
    
    [self disapperView];
}

//消失
- (void)disapperView{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame  = CGRectMake(0, self.Viewheight, self.ViewWidth, self.Viewheight);
        [self removeFromSuperview];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
